<?php
!function_exists('adminmsg') && exit('Forbidden');
$basename="$admin_file?adminjob=tagset";

if(!$action){
	ifcheck($db_iftag,'iftag');
	$db_perpage = 51;
	InitGP(array('page','keyword','ls','le','ns','ne','ifhot'));
	require_once(R_P.'require/forum.php');
	(!is_numeric($page) || $page < 1) && $page = 1;
	$ifhot = (int)$ifhot;
	${'sel_'.$ifhot} = 'selected';
	$sql = "WHERE ifhot='$ifhot'";
	if($keyword){
		$sql .= " AND tagname LIKE '%$keyword%'";
	}
	if($ls>0){
		$sql .= " AND char_length(tagname)>=".(int)$ls;
	}
	if($le>0){
		$sql .= " AND char_length(tagname)<=".(int)$le;
	}
	if($ns>0){
		$sql .= " AND num>=".(int)$ns;
	}
	if($ne>0){
		$sql .= " AND num<=".(int)$ne;
	}
	$limit = "LIMIT ".($page-1)*$db_perpage.",$db_perpage";
	$rt    = $db->get_one("SELECT COUNT(*) AS sum FROM pw_tags $sql");
	$pages = numofpage($rt['sum'],$page,ceil($rt['sum']/$db_perpage), "$basename&keyword=$keyword&ls=$ls&le=$le&ns=$ns&ne=$ne&ifhot=$ifhot&");
	
	$i = ($page-1)*$db_perpage+1;
	$j = 0;
	$tagdb = array();
	$query = $db->query("SELECT * FROM pw_tags $sql ORDER BY num DESC $limit");
	while($rt = $db->fetch_array($query)){
		$tagdb[] = $rt;
	}
	include PrintEot('tagset');exit;
} elseif($action=='tag'){
	InitGP(array('page','tagid','tagname'));
	require_once(R_P.'require/forum.php');
	(!is_numeric($page) || $page < 1) && $page = 1;
	$sql   = is_numeric($tagid) ? "tagid='$tagid'" : "tagname='$tagname'";
	$limit = "LIMIT ".($page-1)*$db_perpage.",$db_perpage";
	$rs    = $db->get_one("SELECT tagid,tagname,num FROM pw_tags WHERE $sql");
	$pages = numofpage($rs['num'],$page,ceil($rs['num']/$db_perpage),"$basename&tagid=$tagid&");
	
	$readb = $ttable_a = array();
	$query = $db->query("SELECT td.tagid,td.tid,t.subject FROM pw_tagdata td LEFT JOIN pw_threads t ON td.tid=t.tid WHERE tagid='$rs[tagid]' $limit");
	while($rt = $db->fetch_array($query)){
		$readb[$rt['tid']] = $rt;
		$ttable_a[GetTtable($rt['tid'])] .= $rt['tid'].',';
	}
	foreach($ttable_a as $pw_tmsgs=>$tids){
		$tids  = substr($tids,0,-1);
		$query = $db->query("SELECT tid,tags FROM $pw_tmsgs WHERE tid IN($tids)");
		while($rt = $db->fetch_array($query)){
			list($tags,$relatetag) = explode("\t",$rt['tags']);
			foreach(explode(' ',$tags) as $key=>$value){
				$readb[$rt['tid']]['tags'] .= "<a href=\"$basename&action=tag&tagname=".rawurlencode($value)."\">$value</a> ";
			}
			foreach(explode(' ',$relatetag) as $key=>$value){
				$readb[$rt['tid']]['relatetag'] .= "<a href=\"$basename&action=tag&tagname=".rawurlencode($value)."\">$value</a> ";
			}
		}
	}
	include PrintEot('tagset');exit;
} elseif($action=='addtag'){
	if(!$_POST['step']){
		include PrintEot('tagset');exit;
	} else{
		InitGP(array('tags'));
		$tagdb = explode(',',$tags);
		foreach($tagdb as $tag){
			if($tag = trim($tag)){
				$rt = $db->get_one("SELECT tagid FROM pw_tags WHERE tagname='$tag'");
				if(!$rt){
					$tag = Char_cv($tag);
					$db->update("INSERT INTO pw_tags (tagname,num) VALUES ('$tag','0')");
				}
			}
		}
		adminmsg('operate_success');
	}
} elseif($_POST['action'] == 'set'){
	InitGP(array('config'));
	foreach($config as $key=>$value){
		$db->pw_update(
			"SELECT * FROM pw_config WHERE db_name='db_$key'",
			"UPDATE pw_config SET db_value='$value' WHERE db_name='db_$key'",
			"INSERT INTO pw_config (db_name,db_value) VALUES('db_$key','$value')"
		);
	}
	updatecache_c();
	adminmsg('operate_success');
} elseif($_POST['action'] == 'deltag'){
	InitGP(array('selid'));
	if(!$selid = checkselid($selid)){
		adminmsg('operate_error');
	}
	$db->update("DELETE FROM pw_tags WHERE tagid IN($selid)");
	$db->update("DELETE FROM pw_tagdata WHERE tagid IN($selid)");
	adminmsg('operate_success');
} elseif($_POST['action']=='sethot'){
	InitGP(array('selid','ifhot'));
	if(!$selid = checkselid($selid)){
		adminmsg('operate_error');
	}
	$ifhot = $ifhot ? 0 : 1;
	$db->update("UPDATE pw_tags SET ifhot='$ifhot' WHERE tagid IN($selid)");
	adminmsg('operate_success');
}
?>